package pers.dawnyang.config.filter.decoder;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import pers.dawnyang.common.util.JsonUtil;

/**
 * 
 * @author dawn yang
 * 
 */
// @Order(1)
// @WebFilter(filterName =
// "DecoderFilter", urlPatterns =
// {"/*"})
// @WebFilter(filterName =
// "Filter1_DecoderFilter", urlPatterns
// = {"/*"})
// @Component
public class DecoderFilter implements Filter {

  private static String aseKey = "1234123412ABCDEF";

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {}

  @Override
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
      FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    DecoderRequestWrapper decoderRequestWrapper = new DecoderRequestWrapper(request);

    String parameter = request.getParameter("userCode");
    String servletPath = request.getServletPath();
    // System.out.println("xxs:" +
    // servletPath);
    // System.out.println("xxsParam:" +
    // parameter);

    String estxt = request.getParameter("estxt");
    Map<String, Object> paramMap = new HashMap<>();
    paramMap.putAll(request.getParameterMap());
    if (StringUtils.isNotBlank(estxt)) {
      String requestParam = null;
      String encodeHexStr = HexUtil.encodeHexStr(aseKey);
      byte[] decodeHex = HexUtil.decodeHex(encodeHexStr);
      AES aes = SecureUtil.aes(decodeHex);
      requestParam = aes.decryptStr(estxt);
      System.out.println("requestParam:" + requestParam);
      paramMap.putAll(dealRequestParamStr(requestParam));
      System.out.println("flag2:" + (dealRequestParamStr(requestParam)));
      paramMap.remove("estxt");
      String[] falg11 = new String[] {"1"};
      paramMap.put("flag11", falg11);
      System.out.println("flag3:" + paramMap);
    }
    decoderRequestWrapper.addAllParameters(paramMap);
    filterChain.doFilter(decoderRequestWrapper, servletResponse);
  }

  @Override
  public void destroy() {

  }

  private Map<String, String> dealRequestParamStr(String requestParamStr) {
    Map<String, String> requestDealMap = new HashMap<String, String>();
    if (requestParamStr.indexOf("&") > 0) {
      requestParamStr = requestParamStr.replaceAll("\"", "");
      try {
        requestParamStr = URLDecoder.decode(requestParamStr, "utf-8");
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      }
      String[] paramArr = requestParamStr.split("&");
      for (String everyParam : paramArr) {
        String[] temp = everyParam.split("=");
        String paramName = temp[0];
        String paramValue = "";
        if (temp.length > 1) {
          paramValue = temp[1];
        }
        requestDealMap.put(paramName, paramValue);
      }
      return requestDealMap;
    }
    return JsonUtil.fromJson(requestParamStr);
  }
}
